package org.ucas.faker.web.utils.tech;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class PatentFeaturizeWithKeywordTree{
	public PatentFeaturizeWithKeywordTree(List<String> branch,List<String> effect){
		kt = new KeywordTree(branch,effect);
		pfRRSingleton = PatentFeaturizeRegularRule.getInstance();
//		System.out.println("\n\n\n###############################\n");
//		System.out.println(kt.branch);
//		System.out.println(kt.effect);
		
	}
	private PatentFeaturizeRegularRule pfRRSingleton = null;
    private KeywordTree kt = null;
	
	public Map<String,String> featurizeAbstractWithRegularRule(String abstra){
		//替换全角；，．等标点
		abstra = Preprocess.replaceSplit(abstra);
		
		//获取分词及标注
		String fenci_res = new CommUtils().postTopic(abstra);
		
//		System.out.println(fenci_res.split("##")[0]+"\n");
		//Map<String,List<String> > map = PatentFeaturize3.getInstance().featurize(s+"  "+res);
        //System.out.println(lineTxt);
        List<String> lkeyword = new ArrayList<>();
        String [] splits = fenci_res.split("##");
        String keywords = splits[0];
        String sent_prop = splits[1];
        for(String s : splits[0].split("\\|")){
        	lkeyword.add(s);
        }        
		
        //获取技术分支与功效
        //使用获得的关键词进行匹配
        Set<String> bra = new HashSet<>();
        Set<String> eff = new HashSet<>();
        kt.classify(abstra, bra, eff,lkeyword);
        
//        System.out.println(abstra);
//        System.out.println(bra);
//        System.out.println(eff);
        
        //调用基于规则的分配算法
		List<String> list =  
				pfRRSingleton.featurizeString(abstra,lkeyword, sent_prop);
		
		//save the results
		Map<String,String> res= new HashMap<>();
		
		//将KeywordTree与featurizeString结果合并
		String branch = list.get(0);
		String effect = list.get(1);
		for(String s:bra) branch = s+"#"+branch;
		for(String s:eff) effect = s+"#"+effect;
		
//		System.out.println(branch);
//		System.out.println(effect);
		if(branch.length()>0&&branch.charAt(0)=='#'){
			branch = branch.substring(1);
		}
		if(effect.length()>0&&effect.charAt(0)=='#'){
			effect = effect.substring(1);
		}
		
		res.put("branch", branch);
		res.put("effect", effect);
		
		return res;
	}

}
